

#include "stdio.h"
#include <jni.h>

#include <utility>
#include "utility.h"

#include <pthread.h>
#include "clMutex.h"

//#include "object_jni.h"
#include "SearchDVS.h"
#include "DeviceManagement.h"

//#include "CameraMediaSource.h"
//#include "VideoPlayer.h"

//#include "PPPP_API.h"



//#include "utils/threads.h"
//#include <pthread.h>

//#include "P2P_API_Define.h"

#ifdef __cplusplus
extern "C" {
#endif


/************************************************************************/
JavaVM* g_JavaVM = NULL;


/************************************************************************/
static CDeviceManagement *g_pPPPDeviceMgt = NULL;


//for cmd thread
JNIEnv	*m_pEnvForCmd = NULL;
jobject g_CmdObj = NULL;

//for video thread
JNIEnv	*m_pEnvForVideo = NULL;
jobject g_VideoObj = NULL;

//for audio thread
JNIEnv	*m_pEnvForAudio = NULL;
jobject g_AudioObj = NULL;


/************************************************************************/
JNIEXPORT void JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_Init(JNIEnv *env ,jobject obj)
{
    Log("===============>>PPPP_GetAPIVersion,: %d",PPPP_GetAPIVersion());
	PPPP_Initialize((CHAR*)"EBGAEOBOKHJMHMJMENGKFIEEHBMDHNNEGNEBBCCCBIIHLHLOCIACCJOFHHLLJEKHBFMPLMCHPHMHAGDHJNNHIFBAMC");
	if (NULL == g_pPPPDeviceMgt)
	{
		g_pPPPDeviceMgt = new CDeviceManagement();
	}
}

JNIEXPORT void JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_Free(JNIEnv *env ,jobject obj)
{
    SAFE_DELETE(g_pPPPDeviceMgt);    
    PPPP_DeInitialize();
}






JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_Login(JNIEnv * env, jobject obj, jobject context_obj, jstring did, jstring user, jstring pwd)
{
    if(g_pPPPDeviceMgt == NULL)    
        return 0;    
    
	m_pEnvForCmd 			= env;
	g_CmdObj 	= env->NewGlobalRef(context_obj);


    char *szDID,*szUser,*szPwd; 
    szDID   =   (char*)env->GetStringUTFChars(did,0);
    szUser   =   (char*)env->GetStringUTFChars(user,0);
    szPwd   =   (char*)env->GetStringUTFChars(pwd,0);
    
    int nRet = g_pPPPDeviceMgt->Login(szDID, szUser, szPwd);

    env->ReleaseStringUTFChars(pwd, szPwd);
    env->ReleaseStringUTFChars(user, szUser);
    env->ReleaseStringUTFChars(did, szDID);

    return nRet;

}

JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_Logout(JNIEnv *env, jobject obj, jint _id)
{

    if(g_pPPPDeviceMgt == NULL)
        return 0;
    
    int nRet = g_pPPPDeviceMgt->Logout(_id);

	if (m_pEnvForCmd != NULL)
	{
		m_pEnvForCmd->DeleteGlobalRef(g_CmdObj);
		g_CmdObj = NULL;
	}
	
    return nRet;
}


/*
//get playactivity object ,method field
void SetPlayWindowContext(JNIEnv *env, jobject context_obj)
{
    //F_LOG;
    
    //::Autolock autoLock(g_PlayContextLock);
    pthread_mutex_lock(&g_PlayContextLock);
    m_pPlayEnv = env;
    g_playobj = env->NewGlobalRef(context_obj);

    pthread_mutex_unlock(&g_PlayContextLock);
}

void SetPlayWindowContextNull()
{
    //F_LOG;
    
    //Mutex::Autolock autoLock(g_PlayContextLock);

    pthread_mutex_lock(&g_PlayContextLock);
    if(m_pPlayEnv)
    {
    	m_pPlayEnv->DeleteGlobalRef(g_playobj);
    	m_pPlayEnv = NULL;
    }
    
//    g_playobj = NULL;
//    g_msgfield = NULL;
//    g_videocallbackfield = NULL;
//    g_CameraParamField = NULL;
//    g_AudioDataCallBackField = NULL;
    pthread_mutex_unlock(&g_PlayContextLock);
}*/



//Request for livestream
JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_StartPPPPLivestream(JNIEnv *env , jobject obj, jobject context_obj, jint _id, jint streamid) 
{
    //F_LOG;
    
    if(g_pPPPDeviceMgt == NULL)
        return 0;

    m_pEnvForVideo 			= env;
    g_VideoObj 	= env->NewGlobalRef(context_obj);


    int nRet = g_pPPPDeviceMgt->RealPlay(_id, streamid);
    return nRet;
}

JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_StopPPPPLivestream(JNIEnv *env , jobject obj, jint _id)
{
    //F_LOG;
    
    if(g_pPPPDeviceMgt == NULL)
        return 0;

    int nRet = g_pPPPDeviceMgt->StopPlay(_id);
	if (m_pEnvForVideo != NULL)
	{
		m_pEnvForVideo->DeleteGlobalRef(g_VideoObj);
		g_VideoObj = NULL;
	}
    return nRet;
}



/*
JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_PPPPStartAudio(JNIEnv *env, jobject obj, jobject context_obj, jint _id)
{
	if(g_pPPPPChannelMgt == NULL)
		return 0;


	if(szDID == NULL)
	{
		env->ReleaseStringUTFChars(did, szDID);
		return 0;
	}    

	return nRet;
}

JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_PPPPStopAudio(JNIEnv *env, jobject obj, jint _id)
{
	if(g_pPPPPChannelMgt == NULL)
		return 0;

	int nRet = g_pPPPPChannelMgt->StopAudio(_id);

	return nRet;
}




JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_PPPPStartTalk(JNIEnv *env, jobject obj, jstring did)
{
    if(g_pPPPPChannelMgt == NULL)
        return 0;
    
    char *szDID;
    szDID   =   (char*)env->GetStringUTFChars(did,0);
    if(szDID == NULL)
    {
        env->ReleaseStringUTFChars(did, szDID);
        return 0;
    }    
    
    int nRet = g_pPPPPChannelMgt->StartTalk(szDID);
    env->ReleaseStringUTFChars(did, szDID);

    return nRet;
}

JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_PPPPStopTalk(JNIEnv *env, jobject obj, jstring did)
{
    if(g_pPPPPChannelMgt == NULL)
        return 0;
    
    char *szDID;
    szDID   =   (char*)env->GetStringUTFChars(did,0);
    if(szDID == NULL)
    {
        env->ReleaseStringUTFChars(did, szDID);
        return 0;
    }    
    
    int nRet = g_pPPPPChannelMgt->StopTalk(szDID);
    env->ReleaseStringUTFChars(did, szDID);

    return nRet;
}

JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_PPPPTalkAudioData(JNIEnv *env, jobject obj, jstring did, jbyteArray audioPCM, jint len)
{
    //F_LOG;
    
    if(g_pPPPPChannelMgt == NULL)
        return 0;
    
    char *szDID;
    szDID   =   (char*)env->GetStringUTFChars(did,0);
    if(szDID == NULL)
    {
        env->ReleaseStringUTFChars(did, szDID);
        return 0;
    }    

    jbyte * audioBuf = (jbyte*)(env->GetByteArrayElements(audioPCM, 0));    
    int nRet = g_pPPPPChannelMgt->TalkAudioData(szDID, (char*)audioBuf, len);

    env->ReleaseByteArrayElements(audioPCM, audioBuf, 0);
    env->ReleaseStringUTFChars(did, szDID);

    return nRet;
}
*/



JNIEXPORT int JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_PPPPNetworkDetect(JNIEnv *env, jobject obj)
{
    st_PPPP_NetInfo NetInfo;
    return PPPP_NetworkDetect(&NetInfo, 0);
}




/************************************************************************/
/*    search                                                            */
/************************************************************************/
// static CSearchDVS *g_PSearchDVS = NULL;
// JNIEXPORT void JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_StartSearch(JNIEnv *env ,jobject obj, jobject context_obj)
// {
// 	SAFE_DELETE(g_PSearchDVS);
// 	g_PSearchDVS = new CSearchDVS();
// 	g_PSearchDVS->Open() ;
// 	return;
// }
// JNIEXPORT void JNICALL Java_object_p2pipcam_nativecaller_NativeCaller_StopSearch(JNIEnv *env ,jobject obj)
// {
// 	SAFE_DELETE(g_PSearchDVS);
// }
// void clean_search()
// {
// 	if(m_pSearchEnv)
// 	{
// 		m_pSearchEnv->DeleteGlobalRef(g_IPCameraClientObj);
// 		m_pSearchEnv = NULL;
// 	}
// 	SAFE_DELETE(g_PSearchDVS);
// }




jint JNI_OnLoad(JavaVM* vm, void* reserved) 
{  
    //F_LOG;

	Log("JNI_OnLoad start");
    g_JavaVM = vm ;


	JNIEnv* env = 0;
	jint result = -1;
	if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) 
    {
		Log("JNI_OnLoad error");
		goto bail;
	}

	result = JNI_VERSION_1_4;    

bail:
	Log("JNI_OnLoad end");
	return result;
}

void JNI_OnUnload(JavaVM *vm, void *reserved)
{
   //F_LOG;
}



#ifdef __cplusplus
}
#endif
